package com.league.zjxn.main.adapter

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.blankj.utilcode.util.ColorUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.league.zjxn.R
import com.league.zjxn.custom.TagTextView
import com.league.zjxn.entity.HomeSearch
import com.league.zjxn.entity.TrendData
import com.league.zjxn.entity.TrendData1
import com.league.zjxn.entity.Trends
import com.league.zjxn.main.home.TrendDetailsActivity
import com.module.lemlin.image.loadCenterImage


class HomeTrendTabAdapter : BaseQuickAdapter<Trends, BaseViewHolder>(R.layout.item_trend_tab) {

    var selectPosition = 0
        set(value) {
            if (field != value) {
                field = value
                notifyDataSetChanged()
            }
        }

    override fun convert(holder: BaseViewHolder, item: Trends) {
        holder.setText(R.id.tvTrendTabName, item.name)
            .getView<View>(R.id.vwTrendTabView).apply {
                visibility = if (holder.layoutPosition == (data.size - 1))
                    View.GONE else View.VISIBLE
            }
        holder.getView<ImageView>(R.id.ivTrendTabIndex).apply {
            visibility = if (selectPosition == holder.layoutPosition)
                View.VISIBLE else View.GONE
        }
    }
}

class HomeTrendTab1Adapter : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_trend_tab_1) {

    var selectPosition = 0
        set(value) {
            if (field != value) {
                field = value
                notifyDataSetChanged()
            }
        }

    override fun convert(holder: BaseViewHolder, item: String) {
        holder.setText(R.id.tvTrendTabName, item)
            .getView<View>(R.id.vwTrendTabView).apply {
                visibility = if (holder.layoutPosition == (data.size - 1))
                    View.GONE else View.VISIBLE
            }
        holder.getView<ImageView>(R.id.ivTrendTabIndex).apply {
            visibility = if (selectPosition == holder.layoutPosition)
                View.VISIBLE else View.GONE
        }
    }
}

class HomeTrendDataAdapter : BaseQuickAdapter<TrendData, BaseViewHolder>(R.layout.item_trend_data) {

    init {
        setOnItemClickListener { adapter, view, i ->
            TrendDetailsActivity.openCon(trendData = getItem(i))
        }
    }

    override fun convert(holder: BaseViewHolder, item: TrendData) {
        holder.getView<TagTextView>(R.id.tvTrendDataName).apply {
            val tagColor = when (item.type) {
                "联盟新闻" -> R.color.FF_2F6CFA
                "通知公告" -> R.color.FF_E24141
                else -> R.color.FF_FA922F
            }
            val tagsBackground = when (item.type) {
                "联盟新闻" -> R.color.FF_E4F1FF
                "通知公告" -> R.color.FF_FFE4E4
                else -> R.color.FF_FFF2E4
            }
            setTextTag(item.title, mutableListOf(item.type), tagColor, tagsBackground)
        }
        holder.setText(R.id.tvTrendDataTime, item.create_time)
            .getView<View>(R.id.vwTrendDataView).apply {
                visibility = if (holder.layoutPosition == (data.size - 1))
                    View.GONE else View.VISIBLE
            }
        holder.getView<ImageView>(R.id.ivTrendDataImage).apply {
            val cover = item.cover
            visibility = if (cover.isNullOrEmpty())
                View.GONE else View.VISIBLE
            if (!cover.isNullOrEmpty()) {
                loadCenterImage(cover)
            }
        }
    }
}

class HomeTrendData1Adapter :
    BaseQuickAdapter<TrendData1, BaseViewHolder>(R.layout.item_trend_data) {

    init {
        setOnItemClickListener { adapter, view, i ->
            TrendDetailsActivity.openCon1(trendData = getItem(i))
        }
    }

    override fun convert(holder: BaseViewHolder, item: TrendData1) {
        holder.getView<TagTextView>(R.id.tvTrendDataName).apply {
            val tagColor = when (item.type) {
                "联盟新闻" -> R.color.FF_2F6CFA
                "通知公告" -> R.color.FF_E24141
                else -> R.color.FF_FA922F
            }
            val tagsBackground = when (item.type) {
                "联盟新闻" -> R.color.FF_E4F1FF
                "通知公告" -> R.color.FF_FFE4E4
                else -> R.color.FF_FFF2E4
            }
            setTextTag(item.title, mutableListOf(item.type), tagColor, tagsBackground)
        }
        holder.setText(R.id.tvTrendDataTime, "${item.years}/${item.day}")
            .getView<View>(R.id.vwTrendDataView).apply {
                visibility = if (holder.layoutPosition == (data.size - 1))
                    View.GONE else View.VISIBLE
            }
        holder.getView<ImageView>(R.id.ivTrendDataImage).apply {
            val cover = item.cover
            visibility = if (cover.isNullOrEmpty())
                View.GONE else View.VISIBLE
            if (!cover.isNullOrEmpty()) {
                loadCenterImage(cover)
            }
        }
    }
}

/**
 * 搜索-热搜-记录关键词适配器
 * */
class HomeSearchLabelAdapter :
    BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_search_label) {
    override fun convert(holder: BaseViewHolder, item: String) {
        holder.setText(R.id.tvSearchLabelName, item)
    }
}

/**
 * 搜索-热搜-记录关键词适配器
 * */
class HomeSearchResultAdapter :
    BaseQuickAdapter<HomeSearch, BaseViewHolder>(R.layout.item_search_result) {
    override fun convert(holder: BaseViewHolder, item: HomeSearch) {
        holder.setText(R.id.tvSearchResultNum, (holder.layoutPosition + 1).toString())
            .setText(R.id.tvSearchResultName, item.title)
            .setText(R.id.tvSearchResultTag,item.type)
        holder.getView<TextView>(R.id.tvSearchResultTag).apply {
            val tagColor = when (item.type) {
                "联盟新闻" -> R.color.FF_2F6CFA
                "通知公告" -> R.color.FF_E24141
                else -> R.color.FF_FA922F
            }
            setTextColor(ColorUtils.getColor(tagColor))
            val tagsBackground = when (item.type) {
                "联盟新闻" -> R.color.FF_E4F1FF
                "通知公告" -> R.color.FF_FFE4E4
                else -> R.color.FF_FFF2E4
            }
            setBackgroundResource(tagsBackground)
        }
//        holder.setText(R.id.tvSearchLabelName, item)
    }
}